package com.lynn.springboot_demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.lynn.springboot_demo.model.Person;
import com.lynn.springboot_demo.task.AsyncTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cl
 * @Date 2020/4/18 16:21
 */
@Controller
@RequestMapping
public class MyController {

    @Autowired
    private AsyncTask asyncTask;

    @GetMapping("/tablePage")
    public String tablePage() {
        //重定向一个html页面必须加/，并且带后缀，配置的视图后缀不会生效
        return "redirect:/bootstrapTable.html";

        //转发就可以直接写html页面的名称，只要配置的视图后缀就不用带后缀
//        return "bootstrapTable";
    }

    /**
     * 异步调用
     *
     * @return
     * @throws Throwable
     */
    @GetMapping("test")
    public String test() throws Throwable {
        long l = System.currentTimeMillis();
        asyncTask.test1();//该方法会执行10秒，通过异步调用可以很快将信息返回客户端，耗时操作后服务器慢慢执行
        /*asyncTask.test2();
        asyncTask.test3();*/
        long l1 = System.currentTimeMillis();
        return "任务总耗时" + (l1 - l) + "毫秒";
    }


    /**
     * bootstrapTable测试返回数据
     * @return
     */
    @GetMapping("/tableList")
    @ResponseBody
    public JSONObject tableList(String name, Integer age) {
        System.out.println("进来了！！！！");
        List<Object> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            Person person = new Person("00" + i, "张三" + i, "程序员" + i, 10 + i);
            list.add(person);
        }
        JSONObject object = new JSONObject();
        object.put("content", list);
        object.put("code", 1);
        object.put("desc", "成功");
        return object;
    }


}
